﻿@using ClothingAggregator.Models.Monitoring
@model dynamic

@{
    ViewBag.Title = "Monitorowanie ogłoszeń";
}
 <link rel="stylesheet" type="text/css" href="@Url.Content("~/Content/Monitoring.css")"/>
 <script type="text/javascript" src="~/Content/Scripts/MonitoringScript.js"></script>

<div class="page">
    <h1 align="center">Monitorowanie ogłoszeń</h1>
    @foreach ( MyMonitoringTask task in ViewBag.Tasks)
    {
        var history = task.History.OrderByDescending(t => t.LastUpdate);
        var lastResult = history.FirstOrDefault();
        if(lastResult == null) {continue;}
        var ad = lastResult.Advertisement;
        
        <div class="monitoringTask" data-id="@task.Id">
            <div class="siteLogo">
                <img src="@Url.Content("~/Content/Img/" + ad.AdSource + ".png")" alt=""/>
            </div>
            <div class="preview">
                <img src="@ad.Photo" height="200" alt="@ad.Title"/>
            </div>       
            <div class="info">
                <span><a href="@ad.Link" target="_blank">@ad.Title</a></span>
                <span>Cena<b> @ad.Price zł </b></span>
                <span>Monitorowany od: @task.CreatedAt</span>
                <span>Ostatnia zmiana: @lastResult.LastUpdate</span>
                <span>Częstotliwość monitorowania: @task.Frequency</span>
                <span>Monitoring Aktywny: <span class="task-active">@(task.Active ? "TAK" : "NIE" )</span></span>
                <button class="activateTask" data-active="@task.Active">@(task.Active ? "Stop" : "Start" )</button>
                <button class="removeTask">Usuń</button>
            </div>
            <div class="history">
                <table>
                    <thead>
                        <tr>
                            <td width="80%">Data</td>
                            <td width="20%">Cena</td>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (var result in history)
                        {
                            <tr>
                                <td >@result.LastUpdate</td>
                                <td>@result.Advertisement.Price zł</td>
                        
                            </tr>
                        }
                    </tbody>
                </table>
            </div>

        </div>
    }
</div>